package kitBox;

import java.io.File;
import java.util.LinkedList;

public class FileListGetter 
{
	public static final int FILE_DIR = 0;
	public static final int FILE_FILE = 1;
	public static final int FILE_ALL = 2;
	public static final int FILE_RECUR_DIR = 3;
	public static final int FILE_RECUR_FILE = 4;
	public static final int FILE_RECUR_ALL = 5;
	
	private static FileListGetter flg = new FileListGetter();
	private LinkedList<File> paths = new LinkedList<File> ();
	
	
	public static FileListGetter getFileListGetter()
	{
		return flg;
	}
	public LinkedList<File> getFileList(String path,int kind , String filter)
	{
		paths = new LinkedList<File> ();
		File file = new File(path);
		switch(kind)
		{
		case FILE_DIR:
		{
			if(file.exists()&& file.isDirectory())
			{
				File[] in = file.listFiles();
				for(int c = 0 ; c < in.length ; c++)
				{
					if(in[c].isDirectory())
					{
						paths.add(in[c]);
					}
				}
			}
			break;
		}
		case FILE_FILE:
		{
			if(file.exists())
			{
				if(file.isDirectory())
				{
					File[] in = file.listFiles();
					for(int c = 0 ; c < in.length ; c++)
					{
						if(!in[c].isDirectory())
						{
							paths.add(in[c]);
						}
					}
				}else
				{
					paths.add(file);
				}
			}
			break;
		}
		case FILE_ALL:
		{
			if(file.exists())
			{
				if(file.isDirectory())
				{
					File[] in = file.listFiles();
					for(int c = 0 ; c < in.length ; c++)
					{
							paths.add(in[c]);
					}
				}else
					paths.add(file);
			}
			break;
		}
		case FILE_RECUR_FILE:
		{
			testShow(file);
			for(int c = 0 ; c < paths.size() ; c++)
			{
				if(paths.get(c).isDirectory())
				{
					paths.remove(c);
					c--;
				}
			}
			break;
		}
		case FILE_RECUR_DIR:
		{
			testShow(file);
			for(int c = 0 ; c < paths.size() ; c++)
			{
				if(!paths.get(c).isDirectory())
				{
					paths.remove(c);
					c--;
				}
			}
			break;
		}
		case FILE_RECUR_ALL:
		{
			testShow(file);
			break;
		}
		}
		
		for(int c = 0 ; c < paths.size() ; c++)
		{
			if(filter == null || filter.equals(""))
			{
				break;
			}else
			{
				if(!paths.get(c).getName().contains(filter))
				{
					paths.remove(c);
					c--;
				}
			}
		}
		return paths;
	}
	public void	testShow(File f)
	{
		File[] inTheF = f.listFiles();
		if(inTheF == null)
		{
			return;
		}
		for(int c = 0 ; c < inTheF.length ; c++)
		{
			if(inTheF[c].isDirectory())
			{
				paths.add(inTheF[c]);
				System.out.println(inTheF[c].getAbsolutePath());
				testShow(inTheF[c]);
			}else
			{
				paths.add(inTheF[c]);
			}
		}
	}
}
